#ifndef COMMON_H
#define COMMON_H

#if defined(_WIN32) || defined(_WIN64)
	#ifdef DEVELOPMENT
		#define MOUDLE_API __declspec(dllexport)
	#else
		#define MOUDLE_API __declspec(dllimport)
	#endif
#else
	#define MOUDLE_API
#endif

#include <iostream>
#include <mutex>
#include <Poco/SharedPtr.h>
#include <Poco/AutoPtr.h>

template<typename T>
class Defer
{
public:
    Defer(const std::shared_ptr<T>& _f)
        :_m(_f)
    {

    }
private:
    std::shared_ptr<T> _m;
};

template<typename T>
class TSingleton
{
protected:
    TSingleton() = default;
    TSingleton(const TSingleton<T>&) = delete;
    TSingleton& operator=(const TSingleton<T>&) = delete;
    static std::shared_ptr<T> _instance;

public:
    static std::shared_ptr<T> instance()
    {
        static std::once_flag s_flag;
        std::call_once(s_flag, [&]()
            {
                _instance = std::shared_ptr<T>(new T);
            });

        return _instance;
    }
    ~TSingleton() = default;
};

template <typename T>
std::shared_ptr<T> TSingleton<T>::_instance = nullptr;

typedef struct _TryOp
{
    int     _maxTryCount = 3;
    bool    _handleall = false;
    int     _millsec = 50;
    bool    _needTry = true;
}TryOp;

typedef struct _SessionOp
{
    int _recvBufferSize = 4096;
    int _sendBufferSize = 4096;
    int _writeQueueSize = 500;
    int     _maxTryCount = 3;
    bool    _handleall = false;
    int     _millsec = 50;
    bool    _needTry = true;
}SessionOp;

typedef struct _BusinessOp
{
    bool _preempt = false;
    int _chunkCount = 10;
}BusinessOp;

typedef struct _HeartbeatOp
{
    int _beatinternal = 30; //s
}HeartbeatOp;

typedef struct _ServerOp
{
    TryOp   _tryop;
    SessionOp   _sessionop;
    BusinessOp  _businessop;
    HeartbeatOp _heartbeatOp;
    bool    _startHeartbeat = false;
    int     _reactorCount = 1;
    int     _businessBSubReactor = 1;
    std::string     _address = "127.0.0.1";
    Poco::UInt16    _port = 8888;
    int _initReactorQueueSize = 3000;
}ServerOp;

#endif // !COMMON_H
